package com.syt.hospital.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.syt.hospital.repository.DepartmentRepository;
import com.syt.hospital.service.DepartmentService;
import com.syt.model.hospital.Department;
import com.syt.vo.hospital.DepartmentQueryVo;
import com.syt.vo.hospital.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: xxx
 * @Date: 2023/2/12 17:28
 * @Description: 科室
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;


    /**
     * 获取科室
     * @param hoscode
     * @param depcode
     * @return
     */
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }

    /**
     * 设置科室名称
     * @param hoscode
     * @param depcode
     * @return
     */
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.findDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department!=null){
            return department.getDepname();
        }
        return null;
    }

    /**
     * 查询医院所有科室列表
     * @param hoscode
     * @return
     */
    @Override
    public List<DepartmentVo> getDepartmentList(String hoscode) {
        //创建list集合 用户最终数据封装
        List<DepartmentVo> result = new ArrayList<>();
        //根据医院编号，查询医院所有科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example example = Example.of(departmentQuery);
        //所有科室列表 departmentList
        List<Department> departmentList = departmentRepository.findAll(example);

        //根据大科室编号 bigcode分组,获取每个大科室下级子科室'
        Map<String, List<Department>>  departmentMap= departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历map集合 departmentMap
        for(Map.Entry<String, List<Department>> entry : departmentMap.entrySet()) {
              //大科室编号
            String bigcode = entry.getKey();

            //大科室编号对应的全局
            List<Department> departmentList1 = entry.getValue();

            //封装大科室
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(bigcode);
            departmentVo1.setDepname(departmentList1.get(0).getDepname());

            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for(Department department : departmentList1){
                DepartmentVo departmentVo2 = new DepartmentVo();
                departmentVo2.setDepcode(department.getDepcode());
                departmentVo2.setDepname(department.getDepname());
                //封装到list集合
                children.add(departmentVo2);
            }
            //把小科室list往大科室children里面
            departmentVo1.setChildren(children);
            //放到最终封装result里面
            result.add(departmentVo1);
        }

        return result;
    }

    /**
     * 删除科室接口
     * @param hoscode
     * @param depcode
     */
    @Transactional
    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department!=null){
            departmentRepository.deleteById(department.getId());
        }
    }

    /**
     * 查询数据并分页
     * @param page
     * @param limit
     * @param departmentQueryVo
     * @return
     */
    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //创建pageable对象，设置当前页和每页记录数
        //0是第一页
        PageRequest pageable = PageRequest.of(page - 1, limit);

        //创建Example对象
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);

        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department, matcher);

        //部门分页查询
        Page<Department> all =departmentRepository.findAll(example, pageable);

        return all;
    }

    /**
     * 调用service，保存科室表中 上传科室接口
     * @param paramMap
     */
    @Transactional
    @Override
    public void saveDepartment(Map<String, Object> paramMap) {
        //paramMap 转换成 Department 对象
        String paramMapString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(paramMapString, Department.class);

        //根据医院编号和科室编号查询
        Department departmentExist= departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());

        //判断存在修改，不存在添加mongoDB
        if (departmentExist!=null){
            departmentExist.setUpdateTime(new Date());
            departmentExist.setIsDeleted(0);
            departmentRepository.save(departmentExist);
        }else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }


    }
}
